package com.sumainfor.pc.gmio.service.impl.tenant;

import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.pc.gmio.dao.tenant.TeWithdrawalMapper;
import com.sumainfor.pc.gmio.feign.BaseTenantFeign;
import com.sumainfor.pc.gmio.feign.Zfeign.ZpcMktFeign;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述:
 * 租客退租账单
 * @Auther: zhlu
 * @Date: 2019/5/18 10:27
 * @Description:
 */
@Service
@Log4j2
public class TeWithdrawalService {

    @Autowired
    TeWithdrawalMapper teWithdrawalMapper;

    @Autowired
    ZpcMktFeign ZpcMktFeign;

    @Autowired
    BaseTenantFeign BaseTenantFeign;

    /**
     * 功能描述: 
     * 根据退租账单编号和租客编号获取退租账单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/18 10:39
     */
    public Map<String,Object> getTanantWithdrawalMap(Map<String,Object>params){
        Map<String,Object>result=new HashMap<>();
        //获取退租账单
        List<Map<String,Object>>TanantWithdrawal=teWithdrawalMapper.getTanantWithdrawalMap(params);
        if(TanantWithdrawal.size()>0){
            //获取房源资料
            JsonResult jsonResult1=ZpcMktFeign.getAllHousingList();
            List<Map<String,Object>>getAllHousingList=(List<Map<String,Object>>)jsonResult1.getResult();

            //租客资料
            JsonResult jsonResult2=BaseTenantFeign.getTenantList();
            List<Map<String,Object>>getTenantList=(List<Map<String,Object>>)jsonResult2.getResult();

            TanantWithdrawal=ListMapMergeUtil.ListMergeUtils(TanantWithdrawal,getAllHousingList,"HouInfoId");
            TanantWithdrawal=ListMapMergeUtil.ListMergeUtils(TanantWithdrawal,getTenantList,"TenId");

            result.putAll(TanantWithdrawal.get(0));


            //根据合同编号获取合同资料
            JsonResult jsonResult=ZpcMktFeign.getPactByPaIdMap(params);
            if(jsonResult.isSuccess()){
                Map<String,Object>getPactByPaIdMap=(Map<String,Object>)jsonResult.getResult();
                result.putAll(getPactByPaIdMap);
            }
        }
        return result;
    }

    /**
     * 功能描述: 
     * 根据退租账单获取退租结算单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/20 10:18
     */
    public Map<String,Object>getTanantWithdrBill(Map<String,Object>params){
        Map<String,Object>getTanantWithdrBill=teWithdrawalMapper.getTanantWithdrBill(params);
        String IsIncome=getTanantWithdrBill.get("IsIncome").toString();
        //退租账单类型    0.唐巢收入    1.唐巢支出
        if(IsIncome.equals("0")||IsIncome=="0"){
            Map<String,Object>getWithByBillMap=teWithdrawalMapper.getWithByBillMap(new HashMap<String,Object>(){{
                put("Source",getTanantWithdrBill.get("WitId"));
            }});
            getTanantWithdrBill.put("billMap",getWithByBillMap);
        }
        return getTanantWithdrBill;
    }

    /**
     * 功能描述: 
     * 根据退租账单编号获取退租物品清单
     * @Author zhlu
     * @Description //TODO
     * @Date 2019/5/20 11:05
     */
    public List<Map<String,Object>>getWithArticleList(Map<String,Object>params){
        List<Map<String,Object>>result=new ArrayList<Map<String,Object>>();

        Map<String,Object>getWithdrMap=teWithdrawalMapper.getWithdrMap(params);

        //根据退租账单编号获取退租物品清单
        List<Map<String,Object>>getWithArticleList=teWithdrawalMapper.getWithArticleList(params);

        //获取房源原始配置
        JsonResult jsonResult=ZpcMktFeign.getWithHouConInfoList(getWithdrMap);
        if(jsonResult.isSuccess()==true){
            //循环房源配置。
            List<Map<String,Object>>getWithHouConInfoList=(List<Map<String,Object>>)jsonResult.getResult();
            for (Map<String,Object> map:getWithHouConInfoList){
                //总物品
                Map<String,Object>totalArticle=new HashMap<String,Object>();
                //已交付物品
                Map<String,Object>consignArticle=new HashMap<String,Object>();
                //损坏物品
                Map<String,Object>damageArticle=new HashMap<String,Object>();
                //物品编号
                String ComId=map.get("ComId").toString();
                //房源退租物品清单大于0
                if(getWithArticleList.size()>0){
                    for(Map<String,Object> map1:getWithArticleList){
                        String ComId1=map1.get("ComId").toString();
                        if(ComId.equals(ComId1)){
                            //总物品数量
                            Integer ComNumber=Integer.valueOf(map.get("ComNumber").toString());
                            //损坏物品数量
                            Integer RecyclingAmount=Integer.valueOf(map1.get("RecyclingAmount").toString());
                            consignArticle.put("ComName",map.get("ComName"));
                            consignArticle.put("ComNumber",ComNumber-RecyclingAmount);

                            damageArticle.put("ComName",map1.get("ComName"));
                            damageArticle.put("ComNumber",map1.get("RecyclingAmount"));
                            break;
                        }else{
                            consignArticle.put("ComName",map.get("ComName"));
                            consignArticle.put("ComNumber",map.get("ComNumber"));
                        }
                    }
                }else{
                    consignArticle.put("ComName",map.get("ComName"));
                    consignArticle.put("ComNumber",map.get("ComNumber"));
                }
                totalArticle.put("ComName",map.get("ComName"));
                totalArticle.put("ComNumber",map.get("ComNumber"));

                Map<String,Object>resultMap=new HashMap<String,Object>();
                resultMap.put("totalArticle",totalArticle);
                resultMap.put("consignArticle",consignArticle);
                resultMap.put("damageArticle",damageArticle);
                result.add(resultMap);
            }
        }
        return result;
    }


}
